const app = getApp()

import {
  goodsDetailRequest
} from '../../api/user'

Page({
  data: {
    autoplay: true,
    interval: 5000,
    duration: 500,
    goodsParameter: '',      // 首页传参 示例 {id: "173"}
    //goodsId: "",            // 商品id  用于拉取商品详情
    active: 0,
    goodsBannerArr: [],     // 商品banner
    goodsName: "",          // 商品名称
    goodsContent: "",       // 套餐内容
    goodsAttention: "",     // 注意事项
    shopKeeperAddress: "",  // 商家地址
    shopKeeperPhone: "",    // 商家电话
    buyStartTime: "",       // 抢购起始时间
    buyEndTime: "",         // 抢购结束时间
    useStartTime: "",       // 使用起始时间
    useEndTime: "",         // 使用起始时间
    soldoutNumber: "",      // 已售数量
    lastNumber: "",         // 库存量
    discountPrice: "",      // 折扣价
    originalPrice: "",      // 原价
    commission: "",         // 佣金
    goodsInfo: "",     // 商品信息
    goodsImage: "",    // 商品主图  
    goodsSelectPackages: '',      // 已选择的套餐  
    dataArr: [{ 'id': '1', 'name': '套餐一' }, { 'id': '2', 'name': '套餐二' }],
    color: "red",
    selectIndex: 0,
    createIndex: '',
    height: '',
    detailHeight: '',
    tempName: '',
    //所有图片的高度  
    imgheights: [],
    //图片宽度 
    imgwidth: 750,
    //默认  
    current: 0,
    iPhoneMargin: app.globalData.isIphone ? 20 : 0,
    totktitCommission:0,
    isShow:true,
    loading:true,
  },

  onLoad: function (option) {

    if (app.globalData.isIphone) {
      tt.showLoading({
        title: '加载中...', // 内容
        success: (res) => {
          
        }
      });
    }

    this.setData({
      goodsParameter: option,
    });

    wx.setNavigationBarTitle({
      title: "详情",
    })

    this.getData();
    this.setData({
      isShow : app.globalData.userType == 0?true:false
    })
    /** 通过分享点击进来的 */
    if (option.hasOwnProperty('shareUser')) {
      app.globalData.shareUser = option.shareUser
    }
  },


  closeItem(){
    this.setData({
      isShow : true
    })
  },

  /** 数据绑定 */
  getData: function () {
    let params = {
      id: this.data.goodsParameter.id,
    };
    goodsDetailRequest(params).then((res) => {
      this.setData({
        goodsImage: res.result.mainPath,    // 商品主图
        goodsInfo: res.result,      // 商品的所有信息
        goodsSelectPackages: res.result.packages[0],
        paths: res.result.paths
      });
      this.clickMenu()
      
    var that = this
    setTimeout(function() {
      that.setData({
        loading:true
      })
    }, 500);
    if (app.globalData.isIphone) {
      tt.hideLoading();
    }
    }).catch((res) => {
      console.log('goodsDetailRequest请求失败')
      if (app.globalData.isIphone) {
        tt.hideLoading();
      }
    })

  },

  /** 爆品首页点击 */
  goHomeClick() {
    tt.switchTab({
      url: `../../pages/homeIndex/homeIndex`
    });
  },

  /** 立即抢购 */
  buyNowClick: function () {
    let shopName = this.data.goodsInfo.agentName
    let goodsImage = this.data.goodsInfo.mainPath
    let goodsName = this.data.goodsInfo.name
    let packages = JSON.stringify(this.data.goodsSelectPackages)
    let goodsId = this.data.goodsInfo.id
    let restrictNum = this.data.restrictNum
    // console.log(shopName)
    // console.log(goodsImage)
    // console.log(goodsName)
    // console.log(packages)
    // console.log(goodsId)
    tt.navigateTo({
      url: `../orderConfirmation/orderConfirmation?shopName=${shopName}&packagesInfo=${packages}&goodsName=${goodsName}&goodsImage=${goodsImage}&goodsId=${goodsId}&restrictNum=${restrictNum}&lastNumber=${this.data.lastNumber}`,        // 指定页面的url
    });
  },

  /** 商品规格 */
  clickMenu(event) {
    var item = event == undefined ? this.data.goodsInfo.packages[0] : event.currentTarget.dataset.item;
    this.setData({
      createIndex: item.id,
      goodsContent: item.content,
      goodsAttention: item.note,
      lastNumber: item.num,
      soldoutNumber: item.soldNum,
      originalPrice: item.price,
      // discountPrice:item.merchantPrice,
      discountPrice: item.platformPrice,
      goodsSelectPackages: item,
      restrictNum:item.restrictNum,
      totktitCommission:item.totktitCommission
    })

  },

  imageLoad: function (e) {//获取图片真实宽度  
    var imgwidth = e.detail.width,
      imgheight = e.detail.height,
      //宽高比  
      ratio = imgwidth / imgheight;
    console.log(imgwidth, imgheight)
    //计算的高度值  
    var viewHeight = 750 / ratio;
    var imgheight = viewHeight;
    var imgheights = this.data.imgheights;
    //把每一张图片的对应的高度记录到数组里  
    imgheights[e.target.dataset.id] = imgheight;
    this.setData({
      imgheights: imgheights
    })
  },
  bindchange: function (e) {
    // console.log(e.detail.current)
    this.setData({ current: e.detail.current })
  },

  /** 触发分享 */
  onShareAppMessage(option) {
    return {
      title: this.data.goodsInfo.title,
      desc: this.data.goodsInfo.title,
      path: `/pages/goodsDetail/goodsDetail?id=${this.data.goodsParameter.id}&shareUser=${tt.getStorageSync('userId')}`, // ?后面的参数会在转发页面打开时传入onLoad方法
      imageUrl: this.data.goodsInfo.mainPath,
      success() {
        console.log('转发发布器已调起，并不意味着用户转发成功，微头条不提供这个时机的回调')
      },
      fail() {
        console.log('转发发布器调起失败');
      }
    }
  },

});   